package movee.domain.constants;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 *
 *
 * @author movee
 */
@Getter
@AllArgsConstructor
public enum FileSyncErrType {

    OK(0, "成功", "success"),
    CONNECT_FAIL(-1, "ftp连接错误", "ftp connection fail"),
    FILE_NOT_EXIST(-2, "文件不存在", "no file on netadmin"),
    FILE_LEN_ZERO(-3, "文件大小为0", "file length is zero"),
    DOWNLOAD_NOT_FULL(-4, "文件传输不完整", "file not completely transferred"),
    DOWNLOAD_EXCEPT(-5, "文件同步异常", "file transferred except");

    private final int id;
    private final String chineseName;
    private final String description;

    private static final Map<String, FileSyncErrType> CHINESE_MAP = new HashMap<>();
    private static final Map<String, FileSyncErrType> NAME_MAP = new HashMap<>();
    private static final Map<String, FileSyncErrType> ID_MAP = new HashMap<>();

    static {
        for (FileSyncErrType status : FileSyncErrType.values()) {
            CHINESE_MAP.put(status.getChineseName(), status);
            NAME_MAP.put(StringUtils.upperCase(status.name()), status);
            ID_MAP.put(String.valueOf(status.id), status);
        }
    }

    public static FileSyncErrType fromChineseName(String chineseName) {
        return chineseName == null ? null : CHINESE_MAP.get(chineseName);
    }

    public static FileSyncErrType fromName(String name) {
        return name == null ? null : NAME_MAP.get(StringUtils.upperCase(name));
    }

    public static FileSyncErrType fromId(String id) {
        return id == null ? null : ID_MAP.get(id);
    }
}
